/*
 * Copyright 2009 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.ui.client.menu;

import java.util.List;

import nl.biz_e.gwt.baselib.client.i18n.Constants;
import nl.biz_e.gwt.baselib.ui.client.IAppManager;
import nl.biz_e.gwt.baselib.ui.client.panels.PanelManager;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Global menu manager
 * 
 * @author Joost A. Bloemsma
 */
public class MenuPanelManager implements IMenuPanelManager {
	private static final String MENUSEPERATOR = " | ";
	private final Anchor signInLink = new Anchor();
	private final Element welcomeElement = RootPanel.get("welcomeText")
			.getElement();
	private final MenuPanel menuPanel;
	private String logInUrl = "";
	private String logOutUrl = "";
	private String logInText = Constants.getBaseLib().logIn();
	private String logOutText = Constants.getBaseLib().logOut();
	private String userName = "";
	private boolean loggedIn = false;
	private final String menuPanelId;
	private final String loginPanelid;
	private final PanelManager panelMgr;
	private final IAppManager appMgr;
	
	/**
	 * Constructs a new MenuPanelManager instance, setting the div id's for the
	 * menupanel and the login link
	 * 
	 * @param menuPanelId
	 * @param loginPanelId
	 * @param panelMgr
	 */
	public MenuPanelManager(String menuPanelId, String loginPanelId,
			PanelManager panelMgr) {
		this.panelMgr = panelMgr;
		this.appMgr = panelMgr.getAppManager();
		this.menuPanelId = menuPanelId;
		this.loginPanelid = loginPanelId;
		menuPanel = new MenuPanel(this);
		this.setLogInUrl("");
		this.setLogOutUrl("");
	}
	
	/**
	 * Initialize this manager. Best do this from the onModuleLoad method
	 */
	public void init() {
		if (this.appMgr.useGoogleLogin()) {
			RootPanel.get(this.loginPanelid).add(signInLink);
		} else {
			IMenuItem login = this.appMgr.getLoginItem();
			RootPanel loginPnl = RootPanel.get(this.loginPanelid);
			if ((login != null) & (loginPnl != null)) {
				loginPnl.add(login.getWidget());
			}
		}
		RootPanel.get(this.menuPanelId).add(menuPanel);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setLogInOutWidget(Widget widget) {
		RootPanel.get(this.loginPanelid).clear();
		RootPanel.get(this.loginPanelid).add(widget);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void addMenuItems(List<IMenuItem> items) {
		if (items == null)
			return;
		if (items.size() > 0) {
			menuPanel.addMenuItems(items);
			menuPanel.rebuildMenu();
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void addMenuItem(IMenuItem menuItem) {
		menuPanel.addMenuItem(menuItem);
		menuPanel.rebuildMenu();
	}
	
	/**
	 * Remove menuitems from the menu
	 * 
	 * @param items
	 */
	public void removeMenuItems(List<IMenuItem> items) {
		if (items == null)
			return;
		if (items.size() > 0) {
			menuPanel.removeMenuItems(items);
			menuPanel.rebuildMenu();
		}
	}
	
	/**
	 * Remove a menuitem from the menu
	 * 
	 * @param menuItem
	 */
	public void removeMenuItem(IMenuItem menuItem) {
		menuPanel.removeMenuItem(menuItem);
		menuPanel.rebuildMenu();
	}
	
	/**
	 * @return the logInUrl
	 */
	public String getLogInUrl() {
		return logInUrl;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setLogInUrl(String logInUrl) {
		this.logInUrl = logInUrl;
		if (!this.isLoggedIn()) {
			signInLink.setHref(logInUrl);
		}
	}
	
	/**
	 * @return the logOutUrl
	 */
	public String getLogOutUrl() {
		return logOutUrl;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setLogOutUrl(String logOutUrl) {
		this.logOutUrl = logOutUrl;
		if (this.isLoggedIn()) {
			signInLink.setHref(logOutUrl);
		}
	}
	
	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public boolean isLoggedIn() {
		return loggedIn;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
		if (loggedIn) {
			welcomeElement.setInnerHTML("<span class=\"nameWelcome\">"
					+ this.getUserName() + "</span>" + MENUSEPERATOR);
			signInLink.setHref(this.getLogOutUrl());
			signInLink.setText(this.getLogOutText());
		} else {
			welcomeElement.setInnerHTML("");
			signInLink.setHref(this.getLogInUrl());
			signInLink.setText(this.getLogInText());
		}
		menuPanel.rebuildMenu();
	}
	
	/**
	 * @return the logInText
	 */
	public String getLogInText() {
		return logInText;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setLogInText(String logInText) {
		this.logInText = logInText;
	}
	
	/**
	 * @return the logOutText
	 */
	public String getLogOutText() {
		return logOutText;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setLogOutText(String logOutText) {
		this.logOutText = logOutText;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setMenuItemSelected(IMenuItem item, boolean selected) {
		this.menuPanel.setMenuItemSelected(item, selected);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setOnlySelectedMenuItem(IMenuItem item) {
		this.menuPanel.setOnlySelectedMenuItem(item);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public IAppManager getAppManager() {
		return this.appMgr;
	}
	
	/**
	 * @return the panelMgr
	 */
	public PanelManager getPanelMgr() {
		return panelMgr;
	}
}
